﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Service.Customer;
using WaterRevenueMS.Core.Repository.Customer;
using WaterRevenueMS.Componnent.Common;
using WaterRevenueMS.Core.Repository.BasicSet;
using WaterRevenueMS.Core.Repository.BaseData;

namespace WaterRevenueMS.Core.Service.BasicSet.Impl
{
    [Export(typeof(ICustomerService))]
    public class CustomerService : CoreServiceBase, ICustomerService
    {
        [Import]
        public ICustomerRepository CustomerRepository { get; set; }

        [Import]
        public IAccountStateRepository AccountStateRepository { get; set; }

        [Import]
        public IRuleOfCodeRepository RuleOfCodeRepository { get; set; }

        [Import]
        public IMeterBookRepository MeterBookRepository { get; set; }

        public IQueryable<Componnent.Entities.Customer.Customer> Customers
        {
            get { return CustomerRepository.Entities; }
        }

        public OperationResult Insert(Domain.ViewModels.Customer.Customer.CustomerModel model)
        {
            GenerateCustomerCode(model);
            var entity = new Componnent.Entities.Customer.Customer
            {
                Yong_HBH = model.Yong_HBH,
                Yong_HZZ = model.Yong_HDZ,
                Zhu_FMJ = model.Zhu_FMJ,
                Hu_ZXM = model.Hu_ZXM,
                Hu_ZXB = model.Hu_ZXB,
                Lian_XDH = model.Lian_XDH,
                Shen_FZH = model.Shen_FZH,
                Jia_TCY = model.Jia_TCY,
                Yin_HSZ_No = model.Yin_HSZ_No!=0?model.Yin_HSZ_No:null,
                Biao_CSZ_No = model.Biao_CSZ_No!=0?model.Biao_CSZ_No:null,
                Yin_HZH = model.Yin_HZH,
                Zhang_HYE = model.Zhang_HYE,
                Zhang_HZT = model.Zhang_HZT,
                Yu_EBJX = model.Yu_EBJX,
                Yu_ETYX = model.Yu_ETYX,
                OrderNo = model.OrderNo,
                CreateDT=DateTime.Now,
                ReviseDT=DateTime.Now
            };
            CustomerRepository.Insert(entity,false);
            UnitOfWork.Commit();
            return new OperationResult(OperationResultType.Success,"添加成功");
        }

        public OperationResult Update(Domain.ViewModels.Customer.Customer.CustomerModel model)
        {
            var entity = CustomerRepository.Entities.FirstOrDefault(t => t.Id == model.Id);
            if (string.IsNullOrEmpty(model.Yong_HBH))
            {
                GenerateCustomerCode(model);
            }
            if (entity != null)
            {
                entity.Yong_HBH = model.Yong_HBH;
                entity.Yong_HZZ = model.Yong_HDZ;
                entity.Zhu_FMJ = model.Zhu_FMJ;
                entity.Hu_ZXM = model.Hu_ZXM;
                entity.Hu_ZXB = model.Hu_ZXB;
                entity.Lian_XDH = model.Lian_XDH;
                entity.Shen_FZH = model.Shen_FZH;
                entity.Jia_TCY = model.Jia_TCY;
                entity.Yin_HSZ_No = model.Yin_HSZ_No!=0?model.Yin_HSZ_No:null;
                entity.Biao_CSZ_No = model.Biao_CSZ_No!=0?model.Biao_CSZ_No:null;
                entity.Yin_HZH = model.Yin_HZH;
                entity.Zhang_HYE = model.Zhang_HYE;
                entity.Zhang_HZT = model.Zhang_HZT;
                entity.Yu_EBJX = model.Yu_EBJX;
                entity.Yu_ETYX = model.Yu_ETYX;
                entity.OrderNo = model.OrderNo;
                entity.ReviseDT = DateTime.Now;
            }
            CustomerRepository.Update(entity);
            return new OperationResult(OperationResultType.Success, "更新成功");
        }

        public OperationResult Delete(int Id)
        {
            var entity = CustomerRepository.Entities.FirstOrDefault(t => t.Id == Id);
            if (entity != null)
            {
                CustomerRepository.Delete(entity);
            }
            return new OperationResult(OperationResultType.Success, "删除成功");
        }

        //生成用户编号
        private void GenerateCustomerCode(Domain.ViewModels.Customer.Customer.CustomerModel model)
        {
            var entity = RuleOfCodeRepository.Entities.FirstOrDefault(t => t.Bian_MDX == 1);
            var meterBook = MeterBookRepository.Entities.FirstOrDefault(t => t.Id == model.Biao_CSZ_No);
            long maxCode = entity.Auto_Num;
            string Zi_ZBHCD = "{0:"+string.Empty.PadRight(entity.Zi_ZBHCD, '0')+"}";
            switch (model.RuleOfCode)
            {
                case 0:
                    break;
                case 1:
                    model.Yong_HBH = meterBook.Area.Qu_YBH + meterBook.Biao_CBH + string.Format(Zi_ZBHCD,(maxCode + entity.Zi_ZBHZL));
                    entity.Auto_Num = entity.Auto_Num + entity.Zi_ZBHZL;
                    break;
                case 2:
                    model.Yong_HBH = meterBook.Biao_CBH + string.Format(Zi_ZBHCD, (maxCode + entity.Zi_ZBHZL));
                    entity.Auto_Num = entity.Auto_Num + entity.Zi_ZBHZL;
                    break;
                case 3:
                    model.Yong_HBH = string.Format(Zi_ZBHCD, (maxCode + entity.Zi_ZBHZL));
                    entity.Auto_Num = entity.Auto_Num + entity.Zi_ZBHZL;
                    break;
            }
            
            RuleOfCodeRepository.Update(entity, false);
        }
    }
}
